
#include "ffmpeg_service.h"

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


static FFmpegServiceServiceImpl* insFFmpegService = NULL;
FFmpegServiceServiceImpl* GetFFmpegServiceServiceImpl() {
    if (NULL == insFFmpegService) {
        static FFmpegServiceServiceImpl mInsFFmpegService;
        return &mInsFFmpegService;
    }
    return insFFmpegService;
}

void SetFFmpegServiceServiceImpl(FFmpegServiceServiceImpl* ins) {
    insFFmpegService = ins;
}


void FFmpegServiceServiceImpl::OnInvoke(ffmpeg::CRPCProtocol& request, ffmpeg::CRPCProtocol& response, fn_cpp_invoke on_invoke)
{
    do {

    
        if (request.method() == "/ffmpeg.FFmpegService/SayHello") {

            ffmpeg::HelloRequest req;
            ffmpeg::HelloReply res;

            if (!req.ParseFromString(request.request())) {
                printf("parse fail. [%s]\n", request.method().data());
                response.set_code(ffmpeg::CRPCProtocol_ErrorCode_InvalidArgument);
                response.set_msg("parse protobuf fail " + request.method());
                break;
            }

            ffmpeg::CRPCProtocol_ErrorCode code = this->SayHello(req, res);

            response.set_response(res.SerializeAsString());
            response.set_code(code);

            break;
        }
    
        if (request.method() == "/ffmpeg.FFmpegService/DemuxerOpen") {

            ffmpeg::DemuxerOpenRequest req;
            ffmpeg::DemuxerOpenReply res;

            if (!req.ParseFromString(request.request())) {
                printf("parse fail. [%s]\n", request.method().data());
                response.set_code(ffmpeg::CRPCProtocol_ErrorCode_InvalidArgument);
                response.set_msg("parse protobuf fail " + request.method());
                break;
            }

            ffmpeg::CRPCProtocol_ErrorCode code = this->DemuxerOpen(req, res);

            response.set_response(res.SerializeAsString());
            response.set_code(code);

            break;
        }
    
        if (request.method() == "/ffmpeg.FFmpegService/DemuxerClose") {

            ffmpeg::DemuxerCloseRequest req;
            ffmpeg::DemuxerCloseReply res;

            if (!req.ParseFromString(request.request())) {
                printf("parse fail. [%s]\n", request.method().data());
                response.set_code(ffmpeg::CRPCProtocol_ErrorCode_InvalidArgument);
                response.set_msg("parse protobuf fail " + request.method());
                break;
            }

            ffmpeg::CRPCProtocol_ErrorCode code = this->DemuxerClose(req, res);

            response.set_response(res.SerializeAsString());
            response.set_code(code);

            break;
        }
    
        if (request.method() == "/ffmpeg.FFmpegService/DemuxerReadPacket") {

            ffmpeg::DemuxerReadPacketRequest req;
            ffmpeg::DemuxerReadPacketReply res;

            if (!req.ParseFromString(request.request())) {
                printf("parse fail. [%s]\n", request.method().data());
                response.set_code(ffmpeg::CRPCProtocol_ErrorCode_InvalidArgument);
                response.set_msg("parse protobuf fail " + request.method());
                break;
            }

            ffmpeg::CRPCProtocol_ErrorCode code = this->DemuxerReadPacket(req, res);

            response.set_response(res.SerializeAsString());
            response.set_code(code);

            break;
        }
    

        response.set_code(ffmpeg::CRPCProtocol_ErrorCode_Unimplemented);
        response.set_msg(request.method() + " un implemented");
    }while(0);

    if (on_invoke) {
        on_invoke(request, response);
    }
}



ffmpeg::CRPCProtocol_ErrorCode FFmpegServiceServiceImpl::SayHello(const ffmpeg::HelloRequest& request, ffmpeg::HelloReply& response) {
    printf("FFmpegServiceService::SayHello un implemented\n");
    return ffmpeg::CRPCProtocol_ErrorCode_Unimplemented;;
}



ffmpeg::CRPCProtocol_ErrorCode FFmpegServiceServiceImpl::DemuxerOpen(const ffmpeg::DemuxerOpenRequest& request, ffmpeg::DemuxerOpenReply& response) {
    printf("FFmpegServiceService::DemuxerOpen un implemented\n");
    return ffmpeg::CRPCProtocol_ErrorCode_Unimplemented;;
}



ffmpeg::CRPCProtocol_ErrorCode FFmpegServiceServiceImpl::DemuxerClose(const ffmpeg::DemuxerCloseRequest& request, ffmpeg::DemuxerCloseReply& response) {
    printf("FFmpegServiceService::DemuxerClose un implemented\n");
    return ffmpeg::CRPCProtocol_ErrorCode_Unimplemented;;
}



ffmpeg::CRPCProtocol_ErrorCode FFmpegServiceServiceImpl::DemuxerReadPacket(const ffmpeg::DemuxerReadPacketRequest& request, ffmpeg::DemuxerReadPacketReply& response) {
    printf("FFmpegServiceService::DemuxerReadPacket un implemented\n");
    return ffmpeg::CRPCProtocol_ErrorCode_Unimplemented;;
}






////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





static FFmpegServiceClient insFFmpegServiceClient;
FFmpegServiceClient* GetFFmpegServiceClinet() {
    return &insFFmpegServiceClient;
}

void SetFFmpegServiceClinetInvoke(fn_cpp_invoke invoke) {
    insFFmpegServiceClient.invoke = invoke;
}


ffmpeg::CRPCProtocol_ErrorCode FFmpegServiceClient::m_invoke(const std::string& method, const google::protobuf::Message& request, google::protobuf::Message& response, ffmpeg::CRPCProtocol* out) {
    if (!this->invoke) {
        return ffmpeg::CRPCProtocol_ErrorCode_NotInit;
    }

    ffmpeg::CRPCProtocol req, res;
    req.set_method(method);
    req.set_request(request.SerializeAsString());

    if (!out) {
        out = &res;
    }

    ffmpeg::CRPCProtocol_ErrorCode result = this->invoke(req, *out);
    if (result != ffmpeg::CRPCProtocol_ErrorCode_OK) {
        return result;
    }

    if (out->code() != ffmpeg::CRPCProtocol_ErrorCode_OK) {
        return out->code();
    }

    if (!response.ParseFromString(out->response())) {
        printf("parse fail %s\n", method.data());
        return ffmpeg::CRPCProtocol_ErrorCode_Fail;
    }
    else {
        // printf("[%s] debug: %s\n", method.data(), response.DebugString().data());
    }

    return ffmpeg::CRPCProtocol_ErrorCode_OK;
}


ffmpeg::CRPCProtocol_ErrorCode FFmpegServiceClient::SayHello(const ffmpeg::HelloRequest& request, ffmpeg::HelloReply& response, ffmpeg::CRPCProtocol* out) {
    return this->m_invoke("/ffmpeg.FFmpegService/SayHello", request, response, out);
}

ffmpeg::CRPCProtocol_ErrorCode FFmpegServiceClient::DemuxerOpen(const ffmpeg::DemuxerOpenRequest& request, ffmpeg::DemuxerOpenReply& response, ffmpeg::CRPCProtocol* out) {
    return this->m_invoke("/ffmpeg.FFmpegService/DemuxerOpen", request, response, out);
}

ffmpeg::CRPCProtocol_ErrorCode FFmpegServiceClient::DemuxerClose(const ffmpeg::DemuxerCloseRequest& request, ffmpeg::DemuxerCloseReply& response, ffmpeg::CRPCProtocol* out) {
    return this->m_invoke("/ffmpeg.FFmpegService/DemuxerClose", request, response, out);
}

ffmpeg::CRPCProtocol_ErrorCode FFmpegServiceClient::DemuxerReadPacket(const ffmpeg::DemuxerReadPacketRequest& request, ffmpeg::DemuxerReadPacketReply& response, ffmpeg::CRPCProtocol* out) {
    return this->m_invoke("/ffmpeg.FFmpegService/DemuxerReadPacket", request, response, out);
}





////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

